<!--
 * @Author: hashMi 854059946@qq.com
 * @Date: 2023-10-28 18:20:33
 * @LastEditors: hashMi 854059946@qq.com
 * @LastEditTime: 2023-10-29 12:38:27
 * @FilePath: /guiguzhenxuan/src/views/login/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="login">
    <!-- <div class="login-left"></div>
    <div class="login-right flex-center">
      <div class="login-right-form">
        <h1 class="form-title">Hello</h1>
        <div class="form-welcome">欢迎来到硅谷甄选</div>
        <div class="form-content">
          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="rules"
            :size="formSize"
            status-icon
          >
            <el-form-item prop="name">
              <el-input
                size="large"
                :prefix-icon="User"
                v-model="ruleForm.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                size="large"
                :prefix-icon="Unlock"
                :show-password="true"
                clearable
                v-model="ruleForm.password"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                size="default"
                @click="submitForm(ruleFormRef)"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div> -->
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          class="login_form"
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          :size="formSize"
          status-icon
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              size="large"
              :prefix-icon="User"
              v-model="ruleForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              size="large"
              :prefix-icon="Unlock"
              :show-password="true"
              clearable
              v-model="ruleForm.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              @click="submitForm(ruleFormRef)"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { getTime } from '@/utils/time'
import { ElNotification } from 'element-plus'
import { User, Unlock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import type { loginFormData } from '@/api/user/type'

const userStore = useUserStore()

let router = useRouter()
let $route = useRoute()

const oldUrl = ref<string>('')

const formSize = ref('small')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<loginFormData>({
  username: 'admin',
  password: 'atguigu123',
})

const rules = reactive<FormRules<loginFormData>>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 5, max: 10, message: '账户长度不能小于5位', trigger: 'change' },
  ],
  password: [
    { required: true, message: '请输入用户密码', trigger: 'blur' },
    { max: 15, message: '账户长度不能大于15', trigger: 'change' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        await userStore.userLogin(ruleForm)
        if (oldUrl.value) {
          router.push(oldUrl.value)
          return
        }

        router.push('/')
        ElNotification({
          title: `Hi , ${getTime()}好`,
          message: '欢迎回来',
          type: 'success',
        })
      } catch (error) {
        ElNotification({
          title: '登录失败',
          message: (error as Error).message,
          type: 'error',
        })
      }
    } else {
      ElNotification({
        title: '用户信息校验失败',
        message: '请检查用户信息',
        type: 'error',
      })
      console.log('error submit!', fields)
      formEl.resetFields()
    }
  })
}

onMounted(() => {
  oldUrl.value = $route.query.redirect as string
  console.log('123', $route)
})
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}

:deep(.el-button) {
  width: 100%;
}
</style>
